/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Core;

/**
 *
 * @author Александр
 */
public class HSV extends Object
{
	private double H=0, S=0, V=0;

	public HSV(int H, double S, double V)
	{
		this.H = H % 360;
		this.V = V;
		this.S = S;
	}

	public int toRGB()
	{
		int i;
		double f, p, q, t;
		double r, g, b;
		double h=H, v=V, s=S;

		if( S == 0 ) // achromatic (grey)
		{
			r = g = b = V;
		}
		else
		{
			h /= 60;   // sector 0 to 5
			i = (int) Math.floor( h );
			f = h - i;   // factorial part of h
			p = v * ( 1 - s );
			q = v * ( 1 - s * f );
			t = v * ( 1 - s * ( 1 - f ) );
			switch( i )
			{
				case 0:
					r = v;
					g = t;
					b = p;
					break;
				case 1:
					r = q;
					g = v;
					b = p;
					break;
				case 2:
					r = p;
					g = v;
					b = t;
					break;
				case 3:
					r = p;
					g = q;
					b = v;
					break;
				case 4:
					r = t;
					g = p;
					b = v;
					break;
				default:
					r = v;
					g = p;
					b = q;
					break;
			}
		}

		int R=(int) (r*255), G=(int)(g*255), B=(int)(b*255);
		return (R<<16)|(G<<8)|B;
	}
}
